#pragma once

#include "map/griddefines.h"

class Map;
namespace battle::object
{
    class WorldObject;
}

struct CellArea
{
    CellArea() {}
    CellArea(CellPair low, CellPair high) : low_bound(low), high_bound(high) {}

    bool operator!() const { return low_bound == high_bound; }

    void ResizeBorders(CellPair& begin_cell, CellPair& end_cell) const
    {
        begin_cell = low_bound;
        end_cell = high_bound;
    }

    CellPair low_bound;
    CellPair high_bound;
};

struct Cell
{
    Cell() { data.All = 0; }
    Cell(const Cell& cell) { data.All = cell.data.All; }
    explicit Cell(CellPair const& p);

    void Compute(unsigned int& x, unsigned int& y) const
    {
        x = data.Part.grid_x * MAX_NUMBER_OF_CELLS + data.Part.cell_x;
        y = data.Part.grid_y * MAX_NUMBER_OF_CELLS + data.Part.cell_y;
    }

    bool DiffCell(const Cell& cell) const
    {
        return(data.Part.cell_x != cell.data.Part.cell_x ||
            data.Part.cell_y != cell.data.Part.cell_y);
    }

    bool DiffGrid(const Cell& cell) const
    {
        return(data.Part.grid_x != cell.data.Part.grid_x ||
            data.Part.grid_y != cell.data.Part.grid_y);
    }

    unsigned int CellX() const { return data.Part.cell_x; }
    unsigned int CellY() const { return data.Part.cell_y; }
    unsigned int GridX() const { return data.Part.grid_x; }
    unsigned int GridY() const { return data.Part.grid_y; }
    bool NoCreate() const { return data.Part.nocreate; }
    void SetNoCreate() { data.Part.nocreate = 1; }

    GridPair gridPair() const { return GridPair(GridX(), GridY()); }

    CellPair cellPair() const
    {
        return CellPair(
            data.Part.grid_x * MAX_NUMBER_OF_CELLS + data.Part.cell_x,
            data.Part.grid_y * MAX_NUMBER_OF_CELLS + data.Part.cell_y);
    }

    Cell& operator=(const Cell& cell)
    {
        data.All = cell.data.All;
        return *this;
    }

    bool operator==(const Cell& cell) const { return (data.All == cell.data.All); }
    bool operator!=(const Cell& cell) const { return !operator==(cell); }
    union
    {
        struct
        {
            unsigned grid_x : 6;
            unsigned grid_y : 6;
            unsigned cell_x : 6;
            unsigned cell_y : 6;
            unsigned nocreate : 1;
            unsigned reserved : 7;
        } Part;
        unsigned int All;
    } data;

    template<class T, class CONTAINER> void Visit(const CellPair& cellPair, TypeContainerVisitor<T, CONTAINER>& visitor, Map& m, float x, float y, float radius) const;
    template<class T, class CONTAINER> void Visit(const CellPair& cellPair, TypeContainerVisitor<T, CONTAINER>& visitor, Map& m, battle::object::WorldObject& obj, float radius) const;

    static CellArea CalculateCellArea(float x, float y, float radius);

    template<class T> static void VisitGridObjects(battle::object::WorldObject* obj, T& visitor, float radius, bool dont_load = true);
    template<class T> static void VisitWorldObjects(battle::object::WorldObject* obj, T& visitor, float radius, bool dont_load = true);
    template<class T> static void VisitAllObjects(battle::object::WorldObject* obj, T& visitor, float radius, bool dont_load = true);

    template<class T> static void VisitGridObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load = true);
    template<class T> static void VisitWorldObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load = true);
    template<class T> static void VisitAllObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load = true);

private:
    template<class T, class CONTAINER> void VisitCircle(TypeContainerVisitor<T, CONTAINER>&, Map&, const CellPair&, const CellPair&) const;
};

